import { http } from '@/request/http'
import type { MakeOptional } from '@/type'

type BaseRole = {
  // 主键（自增）
  id: number
  // 角色名称
  name: string
  // 角色经验
  experience: number
  // 权限列表
  permission: string
  // 状态
  status: boolean
}

export type RoleVO = BaseRole & {
  // 创建时间
  createTime: Date
}

export type RoleDTO = MakeOptional<BaseRole>

export const initRoleVO = () => {
  return {
    id: 0,
    name: '',
    experience: 0,
    createTime: new Date(),
    permission: '',
    status: true
  }
}

export const initRoleDTO = () => {
  return {
    id: undefined,
    name: undefined,
    experience: undefined,
    permission: undefined,
    status: undefined
  }
}

export const roleVO2DOTO = (vo: RoleVO): RoleDTO => {
  return {
    id: vo.id,
    name: vo.name,
    experience: vo.experience,
    permission: vo.permission,
    status: vo.status
  }
}

export const getRoleListApi = () => {
  return http<RoleVO[]>({
    url: '/admin/role/list',
    method: 'GET'
  })
}

export const getRolePermissionMapApi = () => {
  return http<Map<string, string[]>>({
    url: '/admin/role/permission/map',
    method: 'GET'
  })
}

export const updateRoleApi = (dto: RoleDTO) => {
  return http<boolean>({
    url: '/admin/role/update',
    method: 'PUT',
    data: dto
  })
}
